package edu.uiowa.cs.coqview;

import java.io.*;

public class VernacularFileWrapper {
	
	private BufferedReader in;
//	private BufferedWriter out;
	
	VernacularFileWrapper(String path) throws IOException {
		in = new BufferedReader(new FileReader(path));
	}
	
	public void readAndPrint() throws IOException {
		int c;
		while ( (c = in.read()) != -1 ) {
			System.out.print((char)c);
		}
	}
	
	public void closeStreams() throws IOException {
		in.close();
	}
	
	public int getNextCommand(StringBuffer cmd) throws IOException {
		boolean readingComment = false;
		int c;
		int prevc = -1;
		cmd.replace(0, cmd.length(), "");
		
		do {
			c = in.read();
			if (c == -1)
				return -1;
			cmd.append((char)c);
			
			if (readingComment) {
				if ( prevc == '*' && c == ')' )
					readingComment = false;
			} else {
				if (prevc == '(' && c == '*')
					readingComment = true;
			}
	
			prevc = c;
		} while ((char)c != '.' || readingComment);
		
		//To do:  Remove all newlines and then add a newline to the end
		
		return 0;
	}
	
	//Remove newline characters from argument and then append a newline to its end
	public void processCommand(StringBuffer cmd) {
		for (int i = cmd.length() - 1; i >= 0; i--)
			if (cmd.charAt(i)=='\n')
				cmd.deleteCharAt(i);
		cmd.append('\n');
	}
	
	public static void main(String[] args) throws IOException {
		StringBuffer sb = new StringBuffer("");
		
		VernacularFileWrapper vfw = 
			new VernacularFileWrapper("/Users/bnjmnbrmn/Desktop"
					+ "/coqtutorial.v");
	
		//vfw.readAndPrint();
		
		while (vfw.getNextCommand(sb) != -1)
			System.out.println(sb);
		
		sb = new StringBuffer("rewq \n \nasdf vdsz.");
		vfw.processCommand(sb);
		System.out.println(sb);
		
		
		vfw.closeStreams();
		
	}
}
